package com.eshop.common.config;

import com.eshop.common.condition.HttpPathCheckCondition;
import com.eshop.common.strategy.uploadImg.LocalUpload;
import com.eshop.common.strategy.uploadImg.NginxUpload;
import com.eshop.common.strategy.uploadImg.upLoadImg;
import com.eshop.controller.FileController;
import com.eshop.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

//@Configuration
public class FindImgConfig {
    @Value("${file.httpPath}")
    private String httpPath;

    @Bean
    public upLoadImg httpPath(){
        if(StringUtils.isBlank(httpPath) || !TestConnection()) return new LocalUpload();
        return new NginxUpload();
    }

//唯独不能注入controller，没用的
//    @Bean
//    @Conditional(HttpPathCheckCondition.class)
//    public FileController fileController(){
//        return new FileController();
//    }

    public static boolean TestConnection() {
        HttpURLConnection httpURLConnection = null;
        try {
            URL url = new URL("http://110.40.176.175:91/static/api/file/pic/20210706113002899088.jpg");
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(3000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("GET");
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode != 200) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


}
